﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RMS.Utils.MVP;
using RMS.Web.Shell.View;
using RMS.DataAccess.Repositories;
using RMS.Utils.Authentication;
using RMS.DataAccess.Entities;

namespace RMS.Web.Shell.Presenter
{
    public class ChangePasswordPresenter : Presenter<IChangePasswordView>
    {
        EmployeesRepository _employeesRepository;
        IAuthentication _iAuthentication;
        Employees _employee;
        public ChangePasswordPresenter(EmployeesRepository employeesRepository, IAuthentication iAuthentication)
        {
            _employeesRepository = employeesRepository;
            _iAuthentication = iAuthentication;
        }

        public bool ChangePassword(string OldPassword, string NewPassword)
        {
            try
            {
                _employee = _employeesRepository.Find(items =>
                {
                    items.Employee_id = _iAuthentication.CurrentLogedInMember.Id;
                    items.Password = OldPassword.Hash();
                }).Single();
                _employee.Password = NewPassword.Hash();
                _employeesRepository.Update(_employee);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
